<?php
namespace bee;
define("BEE_JS_CLEAR_DEFAULT", 1);
define("BEE_JS_CLEAR_REQUEST", 2);
define("BEE_JS_CLEAR_ALL",     3);

class linkManager extends singleton
{ protected static $__instance;
  protected        $jsPagesArr    = array(  "default"     => array(),     "request"     => array(),
                                            "defaultAttr" => array(),     "requestAttr" => array());

  protected        $linkPagesArr  = array(  "default"     => array(),     "request"     => array(),
      "defaultAttr" => array(),     "requestAttr" => array());

  //protected        $isDirty     = true;
  protected        $jsIsDirty     = true;
  protected        $linkIsDirty   = true;

  /**
   * @return bee\jsManager
   */
  public static function getInstance()
  { $classname = get_called_class();
    if(!(static::$__instance instanceof self))
    { if(!(static::$__instance = factory::load("bee_lsManager")))
      { static::$__instance = new $classname();
      }
    }
    $dispatcher = dispatcher::getInstance();
    $dispatcher->addobject(static::$__instance);
    return static::$__instance;
  }

  public function unLoad($evt)
  { factory::save($this, "bee_lsManager");
  }

  public function onDraw($evt)
  { if($this->jsIsDirty)
    { factory::save(XMLDataHub::getInstance()->setArray($this->jsPagesArr, "__jsPages"), "bee_jsPages");
    } else
    { $jsPages = factory::load("bee_jsPages");
      XMLDataHub::getInstance()->appendFragment("/form/arrays", $jsPages);
    }
    $this->jsIsDirty = false;
    //print_r($this->jsPagesArr);
    if($this->linkIsDirty)
    { factory::save(XMLDataHub::getInstance()->setArray($this->linkPagesArr, "__linkPages"), "bee_linkPages");
    } else
    { $linkPages = factory::load("bee_linkPages");
      XMLDataHub::getInstance()->appendFragment("/form/arrays", $linkPages);
    }
    $this->linkIsDirty = false;
  }

  public function addDefaultJS($scriptName, array $attributes = NULL)
  {
    $this->jsPagesArr["default"][$scriptName] = $scriptName;
    $this->jsIsDirty = true;
  }

  public function addDefaultLink($linkName, array $attributes = NULL)
  { $this->linkPagesArr["default"][$linkName] = $linkName;
    $this->linkPagesArr["defaultAttr"][$linkName] = $attributes;
    $this->linkIsDirty = true;
  }


  public function addScript($scriptName, $form, array $attributes = NULL)
  { $this->jsPagesArr["request"][$form][$scriptName] = $scriptName;
    $this->jsIsDirty = true;
  }

  public function addLink($linkName, $form, array $attributes = NULL)
  { $this->linkPagesArr["request"][$form][$linkName] = $linkName;
    $this->linkIsDirty = true;
  }

  public function removeDefaultJS($scriptName)
  { unset($this->jsPagesArr["default"][$scriptName]);
    $this->jsIsDirty = true;
  }

  public function removeDefaultLink($linkName)
  { unset($this->linkPagesArr["default"][$linkName]);
    $this->linkIsDirty = true;
  }

  public function removeScript($form, $action = "*")
  { unset($this->jsPagesArr["request"][$form][$action]);
    $this->jsIsDirty = true;
  }

  public function removeLink($form, $action = "*")
  { unset($this->linkPagesArr["request"][$form][$action]);
    $this->linkIsDirty = true;
  }

  public function clear($range = BEE_JS_CLEAR_ALL)
  { if($range & BEE_JS_CLEAR_DEFAULT == BEE_JS_CLEAR_DEFAULT)
    { $this->jsPagesArr["default"]   = array();
      $this->linkPagesArr["default"] = array();
      $this->linkPagesArr["defaultAttr"] = array();
    }
    if($range & BEE_JS_CLEAR_REQUEST == BEE_JS_CLEAR_REQUEST)
    { $this->jsPagesArr["request"]   = array();
      $this->linkPagesArr["request"] = array();

    }
    $this->jsIsDirty = true;
    $this->linkIsDirty = true;
  }

}